﻿using FPSO.HMI.Views.Dialog;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Services;
using FPSO.Services.Impl;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using Opc.Ua;
using System.Xml;
using FPSO.HMI.Utils;
using System.Management.Instrumentation;
using OpcUaHelper;
using FPSO.HMI.Controls;
using Prism.Services.Dialogs;
using static FPSO.Models.Constant.TankConstant;
using SafeLoad.NControl;
using System.Xml.Linq;
using System.Threading;
using FPSO.Models.ViewModel.TaskManage;
using MySqlX.XDevAPI.Common;
using static FPSO.HMI.Views.Dialog.TaskTankCheckDialog;
using System.Collections.ObjectModel;
using System.Timers;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;

namespace FPSO.HMI.Views
{
    /// <summary>
    /// ExecuteBallastView.xaml 的交互逻辑
    /// </summary>
    public partial class ExecuteBallastView : UserControl
    {
        private DispatcherTimer taskTimer;
        private DispatcherTimer updateTaskQueueTimer;

        //simuTaskTimer为临时下发任务的计时器，用于监控任务状态
        //为防止下发任务始终没有达到目标值，计时器无法停止，设置任务执行时间
        private DispatcherTimer simuTaskTimer = null;
        private int simuTaskTick = 0;
        private int maxSimuTaskTick = 180;
        private int simuTaskTimeSpan = 20;

        private DateTime startTime;
        private TaskTankCalcService taskTankCalcService = null;
        private string[] dataFiles = null;
		/// <summary>
		/// 当前状态
		/// </summary>
		Dictionary<string, double> currentVolmDic;

		/// <summary>
		/// m_CurrentCalcResult记录当前根据实采数据（或者模拟数据）计算的舱室及船舶状态
		/// </summary>
		ManualCalcResult m_CurrentCalcResult = new ManualCalcResult();
		public ExecuteBallastView(int showCargo)
        {
            InitializeComponent();

            startTime = DateTime.MinValue;

            taskTimer = new DispatcherTimer();
            taskTimer.Interval = TimeSpan.FromSeconds(1);
            taskTimer.Tick += TaskTimer_Tick;
            taskTimer.Start();

            if (showCargo == 1)
            {
                oilBallast.canvasOil.Visibility = Visibility.Visible;
                oilBallast.canvasOilTank.Visibility = Visibility.Visible;
                oilBallast.canvasBallast.Visibility = Visibility.Hidden;
                oilBallast.canvasBallastTank.Visibility = Visibility.Hidden;
            }
            else if (showCargo == 2)
            {
                oilBallast.canvasOil.Visibility = Visibility.Hidden;
                oilBallast.canvasOilTank.Visibility = Visibility.Hidden;
                oilBallast.canvasBallast.Visibility = Visibility.Visible;
                oilBallast.canvasBallastTank.Visibility = Visibility.Visible;
            }
            else
            {
                oilBallast.canvasOil.Visibility = Visibility.Visible;
                oilBallast.canvasOilTank.Visibility = Visibility.Visible;
                oilBallast.canvasBallast.Visibility = Visibility.Visible;
                oilBallast.canvasBallastTank.Visibility = Visibility.Visible;
            }
        }

        
        private void TaskQueue_AddTaskAction(object button, int queueId)
        {
            TaskInfoView createView = new TaskInfoView(queueId, new DBTankTaskService());
            try { createView.ShowDialog(); } catch (Exception ex) { return; }

        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            if (!oilBallast.IsInit())
            {
                oilBallast.StartReal();
            }
        }


        private void TaskTimer_Tick(object sender, EventArgs e)
        {
            //待测试
            oilBallast.SetImitateTime(DateTime.Now);

            //DEBUG 暂时封掉右下角其他液舱的数据
            //otherTanksBarlist.ReflushUIValues(DateTime.Now);

			//先获取一次状态，再刷新当前舱室状态
			currentVolmDic = oilBallast.GetTankVolumeDic();
			SafeLoadAlgorithmService algorithmService = SafeLoadAlgorithmService.GetInstance();
			m_CurrentCalcResult = algorithmService.CallManualCalc(currentVolmDic, 3);
		}

		private void oilBallast_OnAddTankTask(string srcTankId)
		{
			if (!AuthUtil.IsHasPermission())
			{
				return;
			}
			//TankLoadTypeSelectDialog tankLoadTypeSelectDialog = new TankLoadTypeSelectDialog();
			//if (tankLoadTypeSelectDialog.ShowDialog() == true)
			//{
			//	if (tankLoadTypeSelectDialog.LoadTypeSelect == 1)
			//	{
			//		//DEBUG 这里仿照压载倒舱，点击可制定货油注入任务，原先制定计划的过程暂时封掉
			//		Dictionary<string, double> currentVolmDic0 = oilBallast.GetTankVolumeDic();
			//		COTLoadTaskDialog loadTaskDialog = new COTLoadTaskDialog(srcTankId, currentVolmDic0, false);
			//		if (loadTaskDialog.ShowDialog() == true)
			//		{
			//			List<ConstantTankInfo> taskInfos = TankConstant.getCOTTank();
			//			Dictionary<string, double> taskTankVolume = currentVolmDic0;
			//			string tar_tankName = loadTaskDialog.g_TargetTank;
			//			double adjustvol = loadTaskDialog.g_Adjustvol;
			//			Int16 srctankno = 0;
			//			for (int i = 0; i < taskInfos.Count; i++)
			//			{
			//				ConstantTankInfo info = taskInfos[i];
			//				string tankname = info.TankName;
			//				if (tankname == srcTankId)
			//				{
			//					string tankId = info.TankID;
			//					taskTankVolume[tankId] += adjustvol;
			//					srctankno = Convert.ToInt16(i + 1);
			//					break;
			//				}
			//			}
			//			SafeLoadAlgorithmService algorithmService = SafeLoadAlgorithmService.GetInstance();
			//			ManualCalcResult calcResult = algorithmService.CallManualCalc(taskTankVolume, 3);
			//			if (calcResult.GetShipCheckStatus() == true)
			//			{
			//				//再次确认
			//				var dialgoResult = MessageBox.Show("完成货油注入任务下发前检查，是否继续?", "提示", MessageBoxButton.YesNo);
			//				if (dialgoResult == MessageBoxResult.Yes)
			//				{
			//					double srctanklevel = calcResult.GetTankCalcStateByName(srcTankId).Inn;
			//					Int16 readflag = 2;
			//					Int16 tanktype = 0;
			//					Int16 taskop = 1;

			//					string taskpre = "Task1CTInstructCache[0]";
			//					string tag0 = "ReadFlag";
			//					string tag1 = "sourceTkNo";
			//					string tag2 = "targetTkNo";
			//					string tag3 = "TkType";
			//					string tag4 = "TargetLevel";
			//					string tag5 = "TaskOp";
			//					string tag6 = "TargetLevelIdx";

			//					string tag_start = "HmiTask1CTInstructStartCmd";
			//					string status_tag0 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag0);
			//					string status_tag1 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag1);
			//					string status_tag2 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag2);
			//					string status_tag3 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag3);
			//					string status_tag4 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag4);
			//					string status_tag5 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag5);
			//					string status_tag6 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag6);
			//					string status_tag_start = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_start);

			//					//注入没有原舱，代码以0代替
			//					try
			//					{
			//						App.UaClient.WriteNode<Int16>(status_tag1, 0);
			//						App.UaClient.WriteNode<Int16>(status_tag2, srctankno);
			//						App.UaClient.WriteNode<Int16>(status_tag3, tanktype);
			//						App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(srctanklevel));

			//						//[实船调试] 在下面一句设断点，逐步调试，看能否正确发出数据
			//						App.UaClient.WriteNode<Int16>(status_tag5, taskop);
			//						Int16 targetLevelIdx = TankConstant.g_TargetCotLevelIdx[srctankno - 1];
			//						App.UaClient.WriteNode<Int16>(status_tag6, targetLevelIdx);
			//						dialgoResult = MessageBox.Show("完成货油注入任务下发，是否确认执行?", "提示", MessageBoxButton.YesNo);
			//						if (dialgoResult == MessageBoxResult.Yes)
			//						{
			//							App.UaClient.WriteNode<Int16>(status_tag0, readflag);
			//							App.UaClient.WriteNode<Boolean>(status_tag_start, true);

			//							string info = string.Format("装载舱室：[{0}] 目标液位[{1:N3}]M", srcTankId, srctanklevel);
			//							CommUtil.AddSystemLog(6, 0, info);
			//							simuTaskTimer = new DispatcherTimer();
			//							simuTaskTimer.Interval = TimeSpan.FromSeconds(simuTaskTimeSpan);
			//							simuTaskTimer.Tick += SimuTaskTimer_Tick1;
			//							simuTaskTimer.Tag = string.Format("{0}:{1:N3}", srcTankId, srctanklevel);
			//							simuTaskTick = 0;
			//							simuTaskTimer.Start();
			//						}
			//					}
			//					catch (Exception ex)
			//					{
			//						CommUtil.AddSystemLog(0, 2, ex.Message);
			//						return;
			//					}
			//				}
			//			}
			//		}
			//	}
			//	else if (tankLoadTypeSelectDialog.LoadTypeSelect == 2)
			//	{
			//		//DEBUG 双击主界面设置舱室调载目标的交互暂时封掉
			//		Dictionary<string, double> currentVolmDic0 = oilBallast.GetTankVolumeDic();
			//		Dictionary<string, double> currentLevelDic0 = oilBallast.GetTankInnDic();
			//		WBTSwapTaskDialog wBTSwapTaskDialog = new WBTSwapTaskDialog(srcTankId, currentVolmDic0, currentLevelDic0, false);
			//		if (wBTSwapTaskDialog.ShowDialog() == true)
			//		{
			//			List<ConstantTankInfo> taskInfos = TankConstant.getCOTTank();
			//			Dictionary<string, double> taskTankVolume = currentVolmDic0;
			//			string tar_tankName = wBTSwapTaskDialog.g_TargetTank;
			//			double adjustvol = wBTSwapTaskDialog.g_Adjustvol;
			//			bool heelChecked = wBTSwapTaskDialog.g_HeelChecked;
			//			Int16 srctankno = 0;
			//			Int16 tartankno = 0;
			//			for (int i = 0; i < taskInfos.Count; i++)
			//			{
			//				ConstantTankInfo info = taskInfos[i];
			//				string tankname = info.TankName;
			//				if (tankname == srcTankId)
			//				{
			//					string tankId = info.TankID;
			//					taskTankVolume[tankId] -= adjustvol;
			//					srctankno = Convert.ToInt16(i + 1);
			//				}
			//				else if (tankname == tar_tankName)
			//				{
			//					string tankId = info.TankID;
			//					taskTankVolume[tankId] += adjustvol;
			//					tartankno = Convert.ToInt16(i + 1);
			//				}
			//			}

			//			SafeLoadAlgorithmService algorithmService = SafeLoadAlgorithmService.GetInstance();
			//			ManualCalcResult calcResult = algorithmService.CallManualCalc(taskTankVolume, 3);
			//			if (calcResult.GetShipCheckStatus() == true)
			//			{
			//				//再次确认
			//				var dialgoResult = MessageBox.Show("完成倒舱任务下发前检查，是否继续?", "提示", MessageBoxButton.YesNo);
			//				if (dialgoResult == MessageBoxResult.Yes)
			//				{
			//					double srctanklevel = calcResult.GetTankCalcStateByName(srcTankId).Inn;
			//					double tartanklevel = calcResult.GetTankCalcStateByName(tar_tankName).Inn;
			//					Int16 readflag = 2;
			//					Int16 tanktype = 0;
			//					Int16 taskop = 3;

			//					string taskpre = "Task3CTInstructCache[0]";
			//					string tag0 = "ReadFlag";
			//					string tag1 = "sourceTkNo";
			//					string tag2 = "targetTkNo";
			//					string tag3 = "TkType";
			//					string tag4 = "TargetLevel";
			//					string tag5 = "TaskOp";
			//					string tag6 = "TargetLevelIdx";

			//					string tag_start = "HmiTask3CTInstructStartCmd";
			//					string status_tag0 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag0);
			//					string status_tag1 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag1);
			//					string status_tag2 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag2);
			//					string status_tag3 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag3);
			//					string status_tag4 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag4);
			//					string status_tag5 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag5);
			//					string status_tag6 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag6);
			//					string status_tag_start = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_start);

			//					try
			//					{
			//						App.UaClient.WriteNode<Int16>(status_tag1, srctankno);
			//						App.UaClient.WriteNode<Int16>(status_tag2, tartankno);
			//						App.UaClient.WriteNode<Int16>(status_tag3, tanktype);
			//						App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(srctanklevel));
			//						//App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(tartanklevel));
			//						App.UaClient.WriteNode<Int16>(status_tag5, taskop);
			//						Int16 targetLevelIdx = TankConstant.g_TargetCotLevelIdx[srctankno - 1];
			//						App.UaClient.WriteNode<Int16>(status_tag6, targetLevelIdx);

			//						dialgoResult = MessageBox.Show("完成倒舱任务下发，是否确认执行?", "提示", MessageBoxButton.YesNo);
			//						if (dialgoResult == MessageBoxResult.Yes)
			//						{
			//							App.UaClient.WriteNode<Int16>(status_tag0, readflag);
			//							App.UaClient.WriteNode<Boolean>(status_tag_start, true);

			//							simuTaskTimer = new DispatcherTimer();
			//							simuTaskTimer.Interval = TimeSpan.FromSeconds(simuTaskTimeSpan);
			//							simuTaskTimer.Tick += SimuTaskTimer_Tick1;
			//							if (heelChecked)
			//							{
			//								ShipDraught realDraught = shipInfo.GetRealShipInfo();
			//								double Dm_ps = realDraught.MidPDraft;
			//								double Dm_sb = realDraught.MidSDraft;
			//								string info = string.Format("装载舱室：[{0}] 当前液位[{1:N3}]M，当前舯左吃水[{2:N3}]M，当前舯右吃水[{3:N3}]M",
			//									tar_tankName, tartanklevel, Dm_ps, Dm_sb);
			//								CommUtil.AddSystemLog(6, 1, info);
			//								simuTaskTimer.Tag = string.Format("Draft:{0}:{1:N3}", tar_tankName, tartanklevel);
			//							}
			//							else
			//							{
			//								string info = string.Format("排载舱室：[{0}] 目标液位[{1:N3}]M，压载舱室：[{2}] 目标液位[{3:N3}]M",
			//								srcTankId, srctanklevel, tar_tankName, tartanklevel);
			//								CommUtil.AddSystemLog(6, 0, info);
			//								simuTaskTimer.Tag = string.Format("{0}:{1:N3}", tar_tankName, tartanklevel);
			//							}
			//							simuTaskTick = 0;
			//							simuTaskTimer.Start();
			//							return;
			//						}
			//					}
			//					catch (Exception ex)
			//					{
			//						CommUtil.AddSystemLog(0, 2, ex.Message);
			//						return;
			//					}
			//				}
			//			}
			//		}
			//	}
			//}
		}

		private void oilBallast_OnAddBwtTankTask(string srcTankId)
		{
			if (!AuthUtil.IsHasPermission())
			{
				return;
			}
			//TankLoadTypeSelectDialog tankLoadTypeSelectDialog = new TankLoadTypeSelectDialog();
			//if (tankLoadTypeSelectDialog.ShowDialog() == true)
			//{
			//	if (tankLoadTypeSelectDialog.LoadTypeSelect == 2)
			//	{
			//		//DEBUG 双击主界面设置舱室调载目标的交互暂时封掉
			//		Dictionary<string, double> currentVolmDic0 = oilBallast.GetTankVolumeDic();
			//		Dictionary<string, double> currentLevelDic0 = oilBallast.GetTankInnDic();
			//		WBTSwapTaskDialog wBTSwapTaskDialog = new WBTSwapTaskDialog(srcTankId, currentVolmDic0, currentLevelDic0, true);
			//		if (wBTSwapTaskDialog.ShowDialog() == true)
			//		{
			//			List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();
			//			Dictionary<string, double> taskTankVolume = currentVolmDic0;
			//			string tar_tankName = wBTSwapTaskDialog.g_TargetTank;
			//			double adjustvol = wBTSwapTaskDialog.g_Adjustvol;
			//			bool heelChecked = wBTSwapTaskDialog.g_HeelChecked;
			//			Int16 srctankno = 0;
			//			Int16 tartankno = 0;
			//			for (int i = 0; i < taskInfos.Count; i++)
			//			{
			//				ConstantTankInfo info = taskInfos[i];
			//				string tankname = info.TankName;
			//				if (tankname == srcTankId)
			//				{
			//					string tankId = info.TankID;
			//					taskTankVolume[tankId] -= adjustvol;
			//					srctankno = Convert.ToInt16(i + 1);
			//				}
			//				else if (tankname == tar_tankName)
			//				{
			//					string tankId = info.TankID;
			//					taskTankVolume[tankId] += adjustvol;
			//					tartankno = Convert.ToInt16(i + 1);
			//				}
			//			}

			//			SafeLoadAlgorithmService algorithmService = SafeLoadAlgorithmService.GetInstance();
			//			ManualCalcResult calcResult = algorithmService.CallManualCalc(taskTankVolume, 3);
			//			if (calcResult.GetShipCheckStatus() == true)
			//			{
			//				//再次确认
			//				var dialgoResult = MessageBox.Show("完成倒舱任务下发前检查，是否继续?", "提示", MessageBoxButton.YesNo);
			//				if (dialgoResult == MessageBoxResult.Yes)
			//				{
			//					double srctanklevel = calcResult.GetTankCalcStateByName(srcTankId).Inn;
			//					double tartanklevel = calcResult.GetTankCalcStateByName(tar_tankName).Inn;
			//					Int16 readflag = 2;
			//					Int16 tanktype = 1;
			//					Int16 taskop = 3;

			//					string taskpre = "Task3BTInstructCache[0]";
			//					string tag0 = "ReadFlag";
			//					string tag1 = "sourceTkNo";
			//					string tag2 = "targetTkNo";
			//					string tag3 = "TkType";
			//					string tag4 = "TargetLevel";
			//					string tag5 = "TaskOp";
			//					string tag6 = "TargetLevelIdx";

			//					string tag_start = "HmiTask3BTInstructStartCmd";
			//					string status_tag0 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag0);
			//					string status_tag1 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag1);
			//					string status_tag2 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag2);
			//					string status_tag3 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag3);
			//					string status_tag4 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag4);
			//					string status_tag5 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag5);
			//					string status_tag6 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag6);
			//					string status_tag_start = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_start);

			//					try
			//					{
			//						App.UaClient.WriteNode<Int16>(status_tag1, srctankno);
			//						App.UaClient.WriteNode<Int16>(status_tag2, tartankno);
			//						App.UaClient.WriteNode<Int16>(status_tag3, tanktype);
			//						App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(srctanklevel));
			//						//App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(tartanklevel));
			//						App.UaClient.WriteNode<Int16>(status_tag5, taskop);
			//						Int16 targetLevelIdx = TankConstant.g_TargetBwtLevelIdx[srctankno - 1];
			//						App.UaClient.WriteNode<Int16>(status_tag6, targetLevelIdx);

			//						dialgoResult = MessageBox.Show("完成倒舱任务下发，是否确认执行?", "提示", MessageBoxButton.YesNo);
			//						if (dialgoResult == MessageBoxResult.Yes)
			//						{
			//							App.UaClient.WriteNode<Int16>(status_tag0, readflag);
			//							App.UaClient.WriteNode<Boolean>(status_tag_start, true);

			//							simuTaskTimer = new DispatcherTimer();
			//							simuTaskTimer.Interval = TimeSpan.FromSeconds(simuTaskTimeSpan);
			//							simuTaskTimer.Tick += SimuTaskTimer_Tick1;
			//							if (heelChecked)
			//							{
			//								ShipDraught realDraught = shipInfo.GetRealShipInfo();
			//								double Dm_ps = realDraught.MidPDraft;
			//								double Dm_sb = realDraught.MidSDraft;
			//								string info = string.Format("装载舱室：[{0}] 当前液位[{1:N3}]M，当前舯左吃水[{2:N3}]M，当前舯右吃水[{3:N3}]M",
			//									tar_tankName, tartanklevel, Dm_ps, Dm_sb);
			//								CommUtil.AddSystemLog(6, 1, info);
			//								simuTaskTimer.Tag = string.Format("Draft:{0}:{1:N3}", tar_tankName, tartanklevel);
			//							}
			//							else
			//							{
			//								string info = string.Format("排载舱室：[{0}] 目标液位[{1:N3}]M，压载舱室：[{2}] 目标液位[{3:N3}]M",
			//								srcTankId, srctanklevel, tar_tankName, tartanklevel);
			//								CommUtil.AddSystemLog(6, 0, info);
			//								simuTaskTimer.Tag = string.Format("{0}:{1:N3}", tar_tankName, tartanklevel);
			//							}
			//							simuTaskTick = 0;
			//							simuTaskTimer.Start();
			//							return;
			//						}
			//					}
			//					catch (Exception ex)
			//					{
			//						CommUtil.AddSystemLog(0, 2, ex.Message);
			//						return;
			//					}
			//				}
			//			}
			//		}
			//	}
			//	else if (tankLoadTypeSelectDialog.LoadTypeSelect == 1)
			//	{
			//		//DEBUG 这里仿照压载倒舱，点击可制定货油注入任务，原先制定计划的过程暂时封掉
			//		Dictionary<string, double> currentVolmDic0 = oilBallast.GetTankVolumeDic();
			//		COTLoadTaskDialog loadTaskDialog = new COTLoadTaskDialog(srcTankId, currentVolmDic0, true);
			//		if (loadTaskDialog.ShowDialog() == true)
			//		{
			//			List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();
			//			Dictionary<string, double> taskTankVolume = currentVolmDic0;
			//			string tar_tankName = loadTaskDialog.g_TargetTank;
			//			double adjustvol = loadTaskDialog.g_Adjustvol;
			//			int loadType = loadTaskDialog.g_LoadType;
			//			if (loadType == 2)
			//				adjustvol = -adjustvol;
			//			Int16 srctankno = 0;
			//			for (int i = 0; i < taskInfos.Count; i++)
			//			{
			//				ConstantTankInfo info = taskInfos[i];
			//				string tankname = info.TankName;
			//				if (tankname == srcTankId)
			//				{
			//					string tankId = info.TankID;
			//					taskTankVolume[tankId] += adjustvol;
			//					srctankno = Convert.ToInt16(i + 1);
			//					break;
			//				}
			//			}
			//			SafeLoadAlgorithmService algorithmService = SafeLoadAlgorithmService.GetInstance();
			//			ManualCalcResult calcResult = algorithmService.CallManualCalc(taskTankVolume, 3);
			//			if (calcResult.GetShipCheckStatus() == true)
			//			{
			//				//再次确认
			//				var dialgoResult = MessageBox.Show("完成压载任务下发前检查，是否继续?", "提示", MessageBoxButton.YesNo);
			//				if (dialgoResult == MessageBoxResult.Yes)
			//				{
			//					double srctanklevel = calcResult.GetTankCalcStateByName(srcTankId).Inn;
			//					Int16 readflag = 2;
			//					Int16 tanktype = 1;
			//					Int16 taskop = Convert.ToInt16(loadType);

			//					string taskpre = "Task1BTInstructCache[0]";
			//					string tag0 = "ReadFlag";
			//					string tag1 = "sourceTkNo";
			//					string tag2 = "targetTkNo";
			//					string tag3 = "TkType";
			//					string tag4 = "TargetLevel";
			//					string tag5 = "TaskOp";
			//					string tag6 = "TargetLevelIdx";

			//					string tag_start = "HmiTask1BTInstructStartCmd";
			//					string status_tag0 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag0);
			//					string status_tag1 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag1);
			//					string status_tag2 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag2);
			//					string status_tag3 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag3);
			//					string status_tag4 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag4);
			//					string status_tag5 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag5);
			//					string status_tag6 = string.Format("{0}{1}.{2}", SysConfig.OPC_ADD_PRE, taskpre, tag6);
			//					string status_tag_start = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_start);

			//					//注入没有原舱，代码以0代替
			//					//排载过程，舱室编号下发至sourceTkNo；tag1
			//					//压载过程，舱室编号下发至targetTkNo。tag2
			//					try
			//					{
			//						if (loadType == 2)
			//						{
			//							App.UaClient.WriteNode<Int16>(status_tag1, srctankno);
			//							App.UaClient.WriteNode<Int16>(status_tag2, 0);
			//						}
			//						else
			//						{
			//							App.UaClient.WriteNode<Int16>(status_tag1, 0);
			//							App.UaClient.WriteNode<Int16>(status_tag2, srctankno);
			//						}

			//						App.UaClient.WriteNode<Int16>(status_tag3, tanktype);
			//						//App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(srctanklevel));
			//						App.UaClient.WriteNode<float>(status_tag4, Convert.ToSingle(srctanklevel));
			//						App.UaClient.WriteNode<Int16>(status_tag5, taskop);

			//						Int16 targetLevelIdx = TankConstant.g_TargetBwtLevelIdx[srctankno - 1];
			//						App.UaClient.WriteNode<Int16>(status_tag6, targetLevelIdx);

			//						dialgoResult = MessageBox.Show("完成压载任务下发，是否确认执行?", "提示", MessageBoxButton.YesNo);
			//						if (dialgoResult == MessageBoxResult.Yes)
			//						{
			//							App.UaClient.WriteNode<Int16>(status_tag0, readflag);
			//							App.UaClient.WriteNode<Boolean>(status_tag_start, true);

			//							string info = string.Format("装载舱室：[{0}] 目标液位[{1:N3}]M", srcTankId, srctanklevel);
			//							CommUtil.AddSystemLog(6, 0, info);
			//							simuTaskTimer = new DispatcherTimer();
			//							simuTaskTimer.Interval = TimeSpan.FromSeconds(simuTaskTimeSpan);
			//							simuTaskTimer.Tick += SimuTaskTimer_Tick1;
			//							simuTaskTimer.Tag = string.Format("{0}:{1:N3}", srcTankId, srctanklevel);
			//							simuTaskTick = 0;
			//							simuTaskTimer.Start();
			//						}
			//					}
			//					catch (Exception ex)
			//					{
			//						CommUtil.AddSystemLog(0, 2, ex.Message);
			//						return;
			//					}
			//				}
			//			}
			//		}
			//	}
			//}
		}
		//private void SimuTaskTimer_Tick1(object sender, EventArgs e)
		//{
		//	string tag = Convert.ToString(simuTaskTimer.Tag);
		//	Dictionary<string, double> currentVolmDic0 = oilBallast.GetTankVolumeDic();
		//	SafeLoadAlgorithmService algorithmService = SafeLoadAlgorithmService.GetInstance();
		//	ManualCalcResult calcResult = algorithmService.CallManualCalc(currentVolmDic0, 3);

		//	bool check = true;
		//	if (tag.Contains("DRAFT"))
		//	{
		//		string tar_tankname = tag.Split(':')[1];
		//		double tar_tanklevel = Convert.ToDouble(tag.Split(':')[1]);
		//		ShipDraught realDraught = shipInfo.GetRealShipInfo();
		//		double Dm_ps = realDraught.MidPDraft;
		//		double Dm_sb = realDraught.MidSDraft;
		//		string info = string.Format("装载舱室：[{0}] 当前液位[{1:N3}]M，当前舯左吃水[{2:N3}]M，当前舯右吃水[{3:N3}]M",
		//			tar_tankname, tar_tanklevel, Dm_ps, Dm_sb);
		//		CommUtil.AddSystemLog(6, 1, info);

		//		bool stop_check = false;
		//		//如果两舯吃水差小于10cm，则返回
		//		if (Math.Abs(Dm_ps - Dm_sb) < 0.1)
		//		{
		//			check = false;
		//			stop_check = true;
		//		}
		//		//很可能采集值波动较大，始终无法落在上条件区间，则通过调载方向判定
		//		else
		//		{
		//			//如果目标舱是左舱，则右排载左压载，原状态为右倾，至左倾时返回
		//			//如果目标舱是右舱，则左排载右压载，原状态为左倾，至右倾时返回
		//			if ((tar_tankname.Contains("P") && (Dm_ps - Dm_sb > 0.1))
		//				|| (tar_tankname.Contains("S") && (Dm_sb - Dm_ps > 0.1)))
		//			{
		//				check = false;
		//				stop_check = true;
		//			}
		//		}
		//		if (stop_check)
		//		{
		//			//string tag_stop = "HMITaskStopCmd";
		//			string tag_stop = "TskInstructResetOp";
		//			string status_tag0 = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_stop);
		//			App.UaClient.WriteNode<Boolean>(status_tag0, true);
		//			CommUtil.AddSystemLog(6, 0, "调载完成");
		//			simuTaskTimer.Stop();
		//		}
		//	}
		//	else
		//	{
		//		string tar_tankname = tag.Split(':')[0];
		//		double tar_tanklevel = Convert.ToDouble(tag.Split(':')[1]);
		//		double cur_tanklevel = calcResult.GetTankCalcStateByName(tar_tankname).Inn;
		//		string info = string.Format("装载舱室：[{0}] 目标液位[{1:N3}]M，当前液位[{2:N3}]M",
		//			tar_tankname, tar_tanklevel, cur_tanklevel);
		//		CommUtil.AddSystemLog(6, 0, info);

		//		if (cur_tanklevel >= tar_tanklevel)
		//		{
		//			check = false;
		//			CommUtil.AddSystemLog(6, 0, "调载完成");
		//		}
		//	}
		//	simuTaskTick++;
		//	if (simuTaskTick > maxSimuTaskTick)
		//		check = false;

		//	if (!check)
		//	{
		//		//此处需要向下位机发送终止当前过程的命令
		//		//string tag_stop = "HMITaskStopCmd";
		//		//string status_tag0 = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_stop);
		//		//App.UaClient.WriteNode<Boolean>(status_tag0, true);
		//		simuTaskTimer.Stop();
		//	}
		//}

		public void SetTankStatus(ManualCalcResult calcResult)
		{
			oilBallast.SetTankStatus(calcResult);
			//otherTanksBarlist.SetTankStatus(calcResult);
		}
	}
}
